package pl.prv.polanie.client.panels.lists;

import pl.prv.polanie.client.dialogs.CreateSubjectNameDialog;
import pl.prv.polanie.client.dto.CSSubjectNameDTO;
import pl.prv.polanie.client.dto.CSVSubjectNameDTO;
import pl.prv.polanie.client.model.Cache;
import pl.prv.polanie.client.model.ViewDataProvider;
import pl.prv.polanie.client.widgets.table.TableSubjectNames;

import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Widget;

public class SubjectNamesList extends ObjectsList {

	TableSubjectNames tableSubjectNames = new TableSubjectNames(this);

	public SubjectNamesList() {
		
		CSSubjectNameDTO[] sns = Cache.getAllSubjectNames();
		
		
		add(tableSubjectNames);
		
		for (int i = 0; i < sns.length; i++) {
			
			CSSubjectNameDTO csCD = sns[i];
			CSVSubjectNameDTO csvCD = ViewDataProvider.getCSVSubjectName(csCD);
			
			tableSubjectNames.addRow(csCD, csvCD);
		}
		
		//tableSubjectNames.sortInitialOrder();
		
		addRecordButton.addClickListener(new ClickListener(){
			public void onClick(Widget sender) {
				CreateSubjectNameDialog ccdg = new CreateSubjectNameDialog(SubjectNamesList.this, null); 
				ccdg.center();
				
				ccdg.setFocus();
			}
		});
		
		removeSelectedRecordButton.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				tableSubjectNames.removeSelectedSubjectNameFromTable();
			}
		});
		
		setupButtons();
	}

	
	public void addCSSubjectName(CSSubjectNameDTO csSubjectName) {
		tableSubjectNames.saveOrUpdateSubjectNameInTable(csSubjectName);
	}

}
